<?php

namespace App\Http\Controllers\Web;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

// 短信包
use App\Services\SmsService;



use Qiniu\Storage\UploadManager;
use Qiniu\Auth;

class HomeController extends Controller
{
    public function __construct() {

    }
    public function home()
    {
//        $userOb=new XiaoMa\UCenterService();
//
//        $getauth=$userOb->getauth(9455);
//
//        dd($getauth);
        return view('web.home.home');
    }

    public function upfile(Request $request)
    {

        $return=array('strings'=>$_POST,'error'=>'0');
        Log::info('上传记录 :',array($request->all()));
        if ($this->request->getMethod() == "POST" && $request->hasFile('img')) {
            $file = $request->file('img');
            $fileName=uniqid().'.'.$file->getClientOriginalExtension();

            $file->storeAs('appupload',$fileName);
//            dd($file);
            $return['files']['imgname'] = array(
                'url' => $fileName,
                'name' => $file->getClientOriginalName(),
                'type' => $file->getMimeType(),
                'size' => $file->getSize(),
            );

        } else {
            $return['error'] ="未上传文件";
        }

        return response()->json($return,200,array('Content-Type' => 'application/json;charset=utf-8'));
    }

// 短信测试
    public function sms()
    {
        $phone = 18706803425;
        $Verify = mt_rand(100000, 999999);

        $data = array((string)$Verify, '5');

        $rest = SmsService::sendSMS($phone, $data);
        if ($rest == null) {
            exit("发送失败!");
        }
        if ($rest->statusCode != 0) {
            exit("发送失败! 状态：" . $rest->statusCode);
        } else {
            exit("发送成功!");
        }
    }
    /***
     * 邮箱验证
     * @param Request $request
     */
    public function cemail(Request $request)
    {
        $key=$request->get('key');
        $key=base64_decode($key);
        $key=explode(',',$key);
        if(!empty($request->get('key')) && $key[0]=='sd899sd8j3k7sd21poi2433'){
            $data['email']=$key[1];
            $data['emailkey']=2;
            $data['email_time']=time();
            $id=$key[2];
            $res = DB::table('users')->where(['id'=>$id])->update($data);
            if($res){
                echo "<script> alert('邮箱认证成功！');parent.location.href='/'; </script>";
            }
        }else{
            echo "<script> alert('邮箱认证失败！');parent.location.href='/'; </script>";
        }
    }
}
